package org.bim.view.drag.design.controller;

import com.alibaba.fastjson.JSONObject;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.bim.view.drag.design.api.req.MapReq;
import org.bim.view.drag.design.api.vo.VisualMapVO;
import org.bim.view.drag.design.dao.VisualMapMapper;
import org.bim.view.drag.design.entity.TVisualMap;
import org.bim.view.drag.design.model.HttpResult;
import org.bim.view.drag.design.model.Page;
import org.bim.view.drag.design.util.MapDataAccessUtil;
import org.bim.view.drag.design.util.PageUtil;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author pengxg
 * @date 2021/12/16 3:56 下午
 */
@Api
@AllArgsConstructor
@RestController
@RequestMapping("/map")
public class VisualMapController {
    private VisualMapMapper visualMapMapper;
    private MapDataAccessUtil mapDataAccessUtil;

    @GetMapping("detail")
    public HttpResult<TVisualMap> detail(TVisualMap req) {
        return HttpResult.success(visualMapMapper.selectOne(req));
    }

    @GetMapping("data")
    public HttpResult<JSONObject> data(@RequestParam String id) {
        TVisualMap visualMap = visualMapMapper.selectByPrimaryKey(id);
        JSONObject data;
        if (Objects.nonNull(visualMap)) {
            data = JSONObject.parseObject(visualMap.getData());
        } else {
            data = new JSONObject();
        }
        return HttpResult.success(data);
    }
    @ApiOperation("根据区域编码获取行政区划数据")
    @GetMapping("data/{code}")
    public HttpResult<JSONObject> dataByCode(@ApiParam(required = true,value = "区域编码") @PathVariable String code,
                                             @ApiParam("区域名称，最好给一下，好保存")@RequestParam(required = false) String name) {
        JSONObject data;
        TVisualMap map = visualMapMapper.findByCode(code);
        if(map==null){
            data =  mapDataAccessUtil.getDataByCode(code);
            if(data==null){
                return HttpResult.fail(214,"地图数据不存在");
            }else{
                TVisualMap map1 =new TVisualMap();
                map1.setCode(code);
                map1.setName(name);
                map1.setData(data.toJSONString());
                visualMapMapper.insertSelective(map1);
            }
        }else {
            data = JSONObject.parseObject(map.getData());
        }
        return HttpResult.success(data);
    }

    @GetMapping("list")
    public HttpResult<Page<VisualMapVO>> page(MapReq req) {
        Page<VisualMapVO> page = PageUtil.doPage(req.toCondition(), () -> {
            Example example = new Example(TVisualMap.class);
            example.selectProperties("id", "name");
            example.createCriteria().andEqualTo("id", req.getId())
                    .andEqualTo("name", req.getName());
            return visualMapMapper.selectByExample(example);
        }, (entitys) -> entitys.stream().map(e -> {
            VisualMapVO vo = new VisualMapVO();
            vo.setId(e.getId());
            vo.setName(e.getName());
            return vo;
        }).collect(Collectors.toList()));

        return HttpResult.success(page);
    }

    @PostMapping("save")
    public HttpResult<String> save(TVisualMap form) {
        visualMapMapper.insertSelective(form);
        return HttpResult.success(form.getId());
    }

    @PostMapping("update")
    public HttpResult<String> update(TVisualMap form) {
        visualMapMapper.updateByPrimaryKeySelective(form);
        return HttpResult.success(form.getId());
    }

    @PostMapping("remove")
    public HttpResult remove(@RequestParam String ids) {
        visualMapMapper.deleteByIds(ids);
        return HttpResult.success();
    }
}
